package com.yc.jpyy.mobile.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.common.Common;
import com.yc.jpyy.mobile.dao.TeacherDao;
import com.yc.jpyy.mobile.service.TeacherService;

@Controller
@RequestMapping(value = "/teacher")
public class UpdateTeacherInfo {
	@Autowired
	private TeacherService teacherService;

	@Autowired
	private TeacherDao teacherDao;
	private static Logger logger = LoggerFactory.getLogger(TeacherLoginController.class);
	
	/**
	 * 修改教练电话
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/updateTeachPhone", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String updateTeachPhone(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 教练ID
			String teacherId = request.getParameter("teacherId");
			// 手机号
			String mobilePhone = request.getParameter("mobilePhone");

			if (teacherId != null && !teacherId.isEmpty()
					&& mobilePhone != null && !mobilePhone.isEmpty()) {
				Map<String, Object> valMap = new HashMap<String, Object>();
				valMap.put("id", teacherId);
				valMap.put("mobilePhone", mobilePhone);
				//修改教练电话
				int stumap = teacherService.updateTeacherPhoneById(valMap);
				if (stumap <= 0) {
					response.setErrorcode(1);
					response.setMessage("修改失败");
				}
			} else {
				response.setErrorcode(1);
				response.setMessage("教练ID不能为空...");
			}
		} catch (Exception e) {
			logger.info("updateTeachPhone:" + e.toString());
			response.setReturnMsg(1, "修改失败！");
		} finally {
			return response.toJSON();
		}
	}

	/**
	 * 修改教练密码
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/updateTeachpassword", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String updateTeachpassword(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 教练ID
			String teacherId = request.getParameter("teacherId");
			// 旧密码
			String oldPwd = request.getParameter("oldPwd");
			// 新密码
			String newsPwd = request.getParameter("newsPwd");
			
			if (teacherId != null && !teacherId.isEmpty() 
					&& oldPwd != null && !oldPwd.isEmpty() 
					&& newsPwd != null && !newsPwd.isEmpty()) {
				//修改密码
				response = teacherService.updateTeacherPassword(teacherId,newsPwd,oldPwd,response);
			} else {
				response.setErrorcode(1);
				response.setMessage("教练ID、旧密码、新密码不能为空...");
			}
		} catch (Exception e) {
			logger.info("updateTeachpassword:" + e.toString());
			response.setReturnMsg(1, "修改失败！");
		} finally {
			return response.toJSON();
		}
	}
	
    /**
	 * 教练忘记密码
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/forgetTeachpassword", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String forgetTeachpassword(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 身份证号
			String IdCardNo = request.getParameter("IdCardNo");
			// 手机号
			String mobilePhone = request.getParameter("mobilePhone");
			
			if (IdCardNo != null && !IdCardNo.isEmpty() 
					&& mobilePhone != null && !mobilePhone.isEmpty()) {
				//忘记密码身份验证
				response = teacherService.forgetTeachpassword(mobilePhone,IdCardNo,response);
			} else {
				response.setErrorcode(1);
				response.setMessage("手机号、身份证号不能为空...");
			}
		} catch (Exception e) {
			logger.info("forgetTeachpassword:" + e.toString());
			response.setReturnMsg(1, "验证失败！");
		} finally {
			return response.toJSON();
		}
	}
	
	
     /**
	 * 教练忘记密码修改密码
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/updateNewPassword", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String updateNewPassword(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 密码
			String password = request.getParameter("password");
			// 教练ID
			String teacherId = request.getParameter("teacherId");
			if (password != null && !password.isEmpty()
					&& teacherId != null && !teacherId.isEmpty()) {
				Map<String,Object> paramsMap = new HashMap<>();
				paramsMap.put("newPwd", Common.genMDInfo(password));
				paramsMap.put("id", teacherId);
				//忘记密码修改密码
				response = teacherService.updateNewPassword(paramsMap,response);
			} else {
				response.setErrorcode(1);
				response.setMessage("必填参数为空...");
			}
		} catch (Exception e) {
			logger.info("updateNewPassword:" + e.toString());
			response.setReturnMsg(1, "修改失败！");
		} finally {
			return response.toJSON();
		}
	}
}
